//--------------------Sonic CD HUD Script---------------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
// Not all of these are used here in the HUD script, some are just used as a way for other objects to globally transmit values
private alias Object.Value0 : Object.Timer     // Initially set via a loop in the UFO script
private alias Object.Value1 : Object.FreddyTimer     // Initially set via a loop in the UFO script
private alias Object.Value3 : Object.FreddyRandom    // Initially set via a loop in the UFO script
private alias Object.Value4 : Object.FreddyAI    // Initially set via a loop in the UFO script
private alias Object.Value5 : Object.ScareTimer    // Initially set via a loop in the UFO script

private alias Object.Value6 : Object.FreddyRise    // Initially set via a loop in the UFO script
private alias Object.Value7 : Object.MoveTime    // Initially set via a loop in the UFO script
private alias Object.Value8 : Object.HeadShake    // Initially set via a loop in the UFO script
private alias Object.Value9 : Object.LeaveTime    // Initially set via a loop in the UFO script
private alias Object.Value11 : Object.Tweak    // Initially set via a loop in the UFO script


// Game Mode Aliases
public value OfficePower = 100;

// Priority
private alias 1 : PRIORITY_ACTIVE

private alias 0 : CAM_SHOWSTAGE_0
private alias 1 : CAM_DININGHALL_1
private alias 2 : CAM_BACKSTAGE_2
private alias 3 : CAM_PIRATESCOVE_3
private alias 4 : CAM_WESTHALL_4
private alias 5 : CAM_CLOSET_5
private alias 6 : CAM_WHALLCORNER_6
private alias 7 : CAM_EASTHALL_7
private alias 8 : CAM_EHALLCORNER_8
private alias 9 : CAM_KITCHEN_9
private alias 10 : CAM_BATHROOMS_10

private alias 15 : FREDDY_OFFICEDOOR
private alias 16 : FREDDY_WAITTOSCARE
private alias 17 : FREDDY_SCARE


private alias object.state : Freddy.State

private alias Object[2].Value2 : Object.ScrollPos


private table HeadRotation_Offset
	25, 0, -25, 0
end table

reserve function Freddy_Laugh

function Freddy_Laugh



rand(temp1, 2)
switch temp1
	case 0
		PlaySfx(SfxName[Freddy1], 0)	
	break	
	case 1
		PlaySfx(SfxName[Freddy2], 0)	
	break	
	case 2
		PlaySfx(SfxName[Freddy3], 0)	
	break	
end switch

end function


event ObjectMain
//-1 is basically the ai level 0 ig
if Object[5].Type == TypeName[Blank Object]
	switch freddy.state
	case CAM_EHALLCORNER_8	//timer will still go if your looking at him, but will fail if so
		Object.FreddyTimer++
		if Object.FreddyTimer >= 300
			if CameraEnabled == 1
				if Object[6].value1 == 8
					Object.FreddyTimer = 0	
				else
					Callfunction(Freddy_Laugh)
					RWindowScare = 1
					//turn off the ligbth
					if Object[2].Value6 == 1
						StopSfx(SfxName[Hum])
						Object[2].Value6 = 0
						PowerUsage--
					end if
					object.alpha = 0
					freddy.state = 15
				end if
			else
				Callfunction(Freddy_Laugh)
				RWindowScare = 1
				//turn off the ligbth
				if Object[2].Value6 == 1
					StopSfx(SfxName[Hum])
					Object[2].Value6 = 0
					PowerUsage--
				end if
				object.alpha = 0
				freddy.state = 15	
			end if	
		end if	
	break	
	case FREDDY_OFFICEDOOR	//at your door

		if RightDoorClosed == 0	
			Object.ScareTimer++
			if Object.ScareTimer >= 350
				Object.ScareTimer = 0
				RWindowScare = 0

				if Object[2].Value6 == 1
					StopSfx(SfxName[Hum])
					Object[2].Value6 = 0
					PowerUsage--
				end if

				object.state++	//enter offfice
			end if
		else
			Object.LeaveTime++
			if Object.LeaveTime == 15
				PlaySfx(SfxName[BlockR], 0)
				Object.FreddyTimer = 0	
				Object.ScareTimer = 0	
				RWindowScare = 0
				object[6].alpha = 355
				freddy.state = 1		
				Object.LeaveTime = 0
			end if				
		end if	
	break		
	case FREDDY_WAITTOSCARE
		Object.ScareTimer++
		temp0 = 500
		rand(temp1, 50)
		temp0 += temp1
		if Object.ScareTimer >= temp0
			StopMusic()
			PlaySfx(SfxName[Jumpscare], 0)	
			object.frame = 11
			PlayerJumpscared = 1
			object.state++
			Object.ScareTimer = 0
		end if
	break
	case FREDDY_SCARE
		PlayerJumpscared = 1
		if Object.FreddyRise > -100
			Object.FreddyRise -= 16
		else
			Object.FreddyRise = -100
		end if
	break
	case 18
		
		object.state++
	break
	case 19

	break
	default

	CheckEqual(CameraEnabled, 0)
	temp0 = checkResult
	CheckNotEqual(Object[6].value1, Freddy.State)
	temp0 |= checkResult			
	if temp0 == 1
		Object.FreddyTimer++
		if Object.FreddyTimer >= 350
	
			Rand(Object.FreddyRandom, 21)
			if Object.FreddyAI > Object.FreddyRandom 
				//these align with the cameras he goes to
				//state 10 is cam 10
				//15+ is at the office watch out
				switch freddy.state
				case CAM_SHOWSTAGE_0
					freddy.state = CAM_DININGHALL_1
				break
				case 1
					freddy.state = CAM_BATHROOMS_10
				break		
				case CAM_BATHROOMS_10
					freddy.state = CAM_KITCHEN_9
				break	
				case CAM_KITCHEN_9
					freddy.state = CAM_EASTHALL_7
				break								
				case CAM_EASTHALL_7
					freddy.state = CAM_EHALLCORNER_8
				break			
				end switch	
				if Object[6].value1 == Freddy.State
					object[6].alpha = 355
				end if
				if freddy.state < 15
					CallFunction(Freddy_Laugh)
				end if
			end if
			Object.FreddyTimer = 0		
		end if
	end if
	break
	end switch

	if object.state < 18
		if OfficePower == 0
			freddy.state = 18
		end if
	end if



end if	
end event

event ObjectDraw

//	DrawNumbers(0, 32, 96, freddy.state, 3, 8, 0)

//	DrawNumbers(0, 64, 96, Object.FreddyTimer, 3, 8, 0)

//	DrawNumbers(0, 96, 96, Object.ScareTimer, 3, 8, 0)


	if Object.Tweak == 1
		rand(Object.HeadShake, 6)
	end if	

	switch freddy.state
	case 17
		object.drawOrder = 5
		if Object.Tweak == 0
			temp0 = screen.xsize
			temp0 -= screen.xsize
			if Object[10].HeadShake > temp0
				Object[10].HeadShake -= 24
			else
				Object.Tweak = 1
			end if
		end if	

		temp1 = Screen.xsize
		temp1 /= 2
		temp1 -= 140

		Object.AnimationTimer += 96
		
		if Object.AnimationTimer > 239
			Object.AnimationTimer -= 240
			
			object.frame++
			if object.frame > 14
				object.frame = 11
			end if
		end if

		temp1 += Object.HeadShake

		DrawSpriteScreenFX(10, FX_SCALE, temp1, Object.FreddyRise)	//body
		DrawSpriteScreenFX(object.frame, FX_SCALE, temp1, Object.FreddyRise)	//head
	break
	case 15
		if object.alpha < 255
			object.alpha += 8
		end if	
		object.inkeffect = 2
		DrawSpriteScreenFX(15, FX_INK, Object.ScrollPos, 0)
	break
	case 16
		if object.alpha > 0
			object.alpha -= 16
		end if	
		object.inkeffect = 2
		DrawSpriteScreenFX(15, FX_INK, Object.ScrollPos, 0)
	break	
	end switch
end event


event ObjectStartup
	
	LoadSpriteSheet("FNAF/Jumpscares.gif")
	
	// Place the HUD object into reserved object slot 4 and initialise its values
	Object[10].Type = TypeName[Freddy]
	
	// The object should always be run, regardless if it's within camera bounds or not
	Object[10].Priority = PRIORITY_ACTIVE
	
	Object[10].DrawOrder = 3

	Object[10].HeadShake = screen.xcenter

	// 0-9 - Numbers 0-9
	SpriteFrame(0, 0, 8, 13, 558, 1)
	SpriteFrame(0, 0, 8, 13, 567, 1)
	SpriteFrame(0, 0, 8, 13, 576, 1)
	SpriteFrame(0, 0, 8, 13, 585, 1)
	SpriteFrame(0, 0, 8, 13, 594, 1)
	SpriteFrame(0, 0, 8, 13, 558, 15)
	SpriteFrame(0, 0, 8, 13, 567, 15)
	SpriteFrame(0, 0, 8, 13, 576, 15)
	SpriteFrame(0, 0, 8, 13, 585, 15)
	SpriteFrame(0, 0, 8, 13, 594, 15)

	
	SpriteFrame(0, 280, 298, 83, 365, 871)
	SpriteFrame(46, 100, 206, 227, 457, 643)	//11
	SpriteFrame(46, 100, 206, 227, 457, 643)	//12
	SpriteFrame(46, 100, 206, 227, 457, 643)	//13
	SpriteFrame(46, 100, 206, 227, 457, 643)	//14

	//fred 26 
	SpriteFrame(440, 67, 42, 126, 981, 1)		

end event


// ========================
// Editor events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("3D_Test/Objects.gif")
	SpriteFrame(0, 0, 24, 16, 206, 58)

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
